package com.jason.lucene.example.index;

import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 * @author he peng
 * @create 2017/11/30 13:45
 * @see
 */
public class DocumentDeleteTest extends BaseIndexingTest {

    @Test
    public void deleteDocument() throws Exception {

        IndexReader indexReader = DirectoryReader.open(super.indexDir);
        System.err.println("index reader delete before , maxDoc = " +
                indexReader.maxDoc() +
                " , numDocs = " + indexReader.numDocs() +
                " , hasDeletions = " + indexReader.hasDeletions());
        indexReader.close();

        IndexWriterConfig conf = new IndexWriterConfig(getAnalyzer());
        IndexWriter indexWriter = new IndexWriter(super.indexDir, conf);

        System.out.println("index writer delete before , maxDoc = " +
                indexWriter.maxDoc() +
                " , numDocs = " + indexWriter.numDocs() +
                " , hasDeletions = " + indexWriter.hasDeletions());

        Term term = new Term("id","2");
        indexWriter.deleteDocuments(term);

        System.out.println("index writer delete after , maxDoc = " +
                indexWriter.maxDoc() +
                " , numDocs = " + indexWriter.numDocs() +
                " , hasDeletions = " + indexWriter.hasDeletions());

        indexReader = DirectoryReader.open(super.indexDir);
        System.err.println("index reader delete after , maxDoc = " +
                indexReader.maxDoc() +
                " , numDocs = " + indexReader.numDocs() +
                " , hasDeletions = " + indexReader.hasDeletions());
        indexReader.close();

        indexWriter.commit();

        System.out.println("index writer commit after , maxDoc = " +
                indexWriter.maxDoc() +
                " , numDocs = " + indexWriter.numDocs() +
                " , hasDeletions = " + indexWriter.hasDeletions());

        indexWriter.close();

        indexReader = DirectoryReader.open(super.indexDir);
        System.err.println("index reader commit after , maxDoc = " +
                indexReader.maxDoc() +
                " , numDocs = " + indexReader.numDocs() +
                " , hasDeletions = " + indexReader.hasDeletions());
        indexReader.close();

    }
}
